<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'basicLineChart'
    },
    width: {
      type: String,
      default: '100%'
    },
    legendName: {
      type: String,
      default: '数量'
    },
    height: {
      type: String,
      default: '500px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    itemColor: {
      type: String,
      default: '#3888fa'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if (this.chart !== null) {
          this.setOptions(val)
        }
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ valueX, valueY1, valueY2, valueY3, title } = {}) {
      this.chart.setOption({
        title: {
          text: title
        },
        xAxis: {
          data: valueX,
          type: 'category'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          type: 'value',
          axisTick: {
            show: false
          }
        },
        series: [
          {
            name: title,
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: this.itemColor,
                lineStyle: {
                  color: 'green',
                  width: 3
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: valueY1
          },
          {
            name: title,
            smooth: false,
            type: 'line',
            symbol: 'diamond',
            itemStyle: {
              normal: {
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                }
              }
            },
            data: valueY2
          },
          {
            name: title,
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: this.itemColor,
                lineStyle: {
                  color: 'green',
                  width: 3
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: valueY3
          }
        ]
      })
    }
  }
}
</script>
